
function layTable(table, tag_id, flag, height, url, title, table_id, cols, ifTotal) {
    table.render({
        elem: tag_id
        , height: height
        , url: url //数据接口
        , title: title
        , page: {theme: '#1E9FFF'} //开启分页
        , toolbar: 'default' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
        , limits: [5, 10, 30, 50, 100]
        , limit: 5
        , defaultToolbar: ['print', 'exports']
        , id: table_id
        , totalRow: ifTotal //开启合计行
        , cols: cols
    });

    //监听头工具栏事件
    table.on('toolbar(' + flag + ')', function (obj) {
        var checkStatus = table.checkStatus(obj.config.id)
            , data = checkStatus.data; //获取选中的数据
        switch (obj.event) {
            case 'add':
                window.location.href = window.location.origin + "/" + location.pathname.split("/")[1] + "/page_" + flag + "s";
                break;
            case 'update':
                if (data.length === 0) {
                    layer.msg('请选择一行');
                } else if (data.length > 1) {
                    layer.msg('只能同时编辑一个');
                } else {
                    window.location.href = window.location.origin + "/" + location.pathname.split("/")[1] + "/page_" + flag + "s?type=edit&id=" + checkStatus.data[0].id;
                    break;
                    //layer.alert('编辑 [id]：' + checkStatus.data[0].id);
                }
                break;
            case 'delete':
                if (data.length === 0) {
                    layer.msg('请选择一行');
                } else {
                    var id = '';
                    for (var i = 0; i < data.length; i++) {
                        id += "'" + data[i].id + "',";
                    }
                    $.ajax({
                        url: '${rootPath}/' + flag + '_delete',
                        data: {ids: id.substr(0, id.length - 1)},
                        dataType: 'json',
                        type: 'post',
                        success : function (data) {
                            table.reload(table_id);
                        }
                    });

                }
                break;
        }
    });

    //监听行工具事件
    table.on('tool(' + flag + ')', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
        var data = obj.data //获得当前行数据
            , layEvent = obj.event; //获得 lay-event 对应的值
        if (layEvent === 'detail') {
            // layer.msg('查看操作');
        } else if (layEvent === 'del') {
            layer.confirm('真的删除行么', function (index) {
                if (index) {
                    $.ajax({
                        url: '${rootPath}/' + flag + '_delete',
                        data: data,
                        dataType: 'json',
                        type: 'post',
                        success: function (data) {
                            console.log(data)
                        }
                    });
                } else {
                    layer.msg('系统错误！');
                }
                obj.del(); //删除对应行（tr）的DOM结构
                layer.close(index);
                //向服务端发送删除指令
            });
        } else if (layEvent === 'edit') {
            window.location.href = window.location.origin + "/" + location.pathname.split("/")[1] + "/page_" + flag + "s?type=edit&id=" + data.id;
        }
    });
    table.on('edit(' + flag + ')', function (obj) {
        //obj.value           得到修改后的值
        //data = obj.data     得到所在行所有键值
        //field = obj.field;  得到字段
        if (obj) {
            $.ajax({
                url: "${rootPath}/" + flag + "_edit",
                dataType: "json",
                type: "post",
                data: obj.data,
                success: function (data) {
                    layer.msg(data.msg);
                }
            });
        }
    });

    //查询
    var $ = layui.$, active = {
        reload: function () {
            var keyword = $('input[name=keyword]').val();
            var condition = null;
            if (/^[1-9]\d*$/.test(keyword)) {
                condition = {id: keyword, cusName: keyword, cusPhone: keyword, cusAddr: keyword};
            } else if (keyword) {
                condition = {id: -1, cusName: keyword, cusPhone: keyword, cusAddr: keyword};
            } else {
                condition = {id: null};
            }
            //执行重载
            table.reload('customer_table', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                , where: condition
            }, 'data');
        }
    };

    $('#search').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    $(document).keydown(function (event) {
        if (event.keyCode == 13) {
            $("#search").trigger('click');
        }
    });
}
